package com.ai.chatai.config;

import com.ai.chatai.constants.SystemConstants;
import com.ai.chatai.tools.CourseTools;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.client.advisor.vectorstore.QuestionAnswerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiEmbeddingModel;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


//配置客户端
@Configuration
public class CommonConfiguration {
    @Bean
    public ChatMemory chatMemory() {
        // 使用 MessageWindowChatMemory 作为默认内存策略（窗口消息保留）
        return MessageWindowChatMemory.builder().build();
    }
    @Bean
    public VectorStore vectorStore(OpenAiEmbeddingModel embeddingModel) {
        return SimpleVectorStore.builder(embeddingModel).build();
    }
    @Bean
    public ChatClient chatClient(OpenAiChatModel model, ChatMemory chatMemory){
        return ChatClient
                .builder(model)
                .defaultOptions(ChatOptions.builder().model("qwen-omni-turbo").build())
                .defaultSystem("你是一个可爱AI助手，你的名字叫饺子，请以小团团的身份和语气来回答问题")
                .defaultAdvisors(new SimpleLoggerAdvisor())  //日志
                .defaultAdvisors(MessageChatMemoryAdvisor.builder(chatMemory).build())// 添加会话记忆
                .build();
    }
    @Bean
    public ChatClient gameChatClient(OpenAiChatModel model, ChatMemory chatMemory){
        return ChatClient
                .builder(model)
                .defaultSystem(SystemConstants.GAME_SYSTEM_PROMPT)
                .defaultAdvisors(new SimpleLoggerAdvisor())  //日志
                .defaultAdvisors(MessageChatMemoryAdvisor.builder(chatMemory).build())// 添加会话记忆
                .build();
    }
    @Bean
    public ChatClient serviceChatClient(OpenAiChatModel model, ChatMemory chatMemory, CourseTools courseTools){
        return ChatClient
                .builder(model)
                .defaultSystem(SystemConstants.SERVICE_SYSTEM_PROMPT)
                .defaultAdvisors(new SimpleLoggerAdvisor())  //日志
                .defaultAdvisors(MessageChatMemoryAdvisor.builder(chatMemory).build())// 添加会话记忆
                .defaultTools(courseTools)
                .build();
    }
    /**
     * 配置pdf文件对话用ChatClient对象，用于处理用户输入的pdf文件，并返回处理结果
     * @param model 使用OpenAI的模型
     * @param chatMemory 会话记忆
     * @param vectorStore 通过向量库进行相似度搜索
     */
    @Bean
    public ChatClient pdfChatClient(OpenAiChatModel model,
                                    ChatMemory chatMemory,
                                    VectorStore vectorStore) {
        return ChatClient.builder(model)// 选择模型
                .defaultSystem(SystemConstants.PDF_SYSTEM_PROMPT)// 系统设置
                .defaultAdvisors(new SimpleLoggerAdvisor())// 添加日志记录
                .defaultAdvisors(MessageChatMemoryAdvisor.builder(chatMemory).build())// 添加会话记忆功能
                .defaultAdvisors(QuestionAnswerAdvisor.builder(vectorStore).// 添加向量库
                        searchRequest(SearchRequest.builder().similarityThreshold(0.6).// 设置相似度阈值
                        topK(2).// 设置返回文档数量
                        build()).build())
                .build();
    }
}
